home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / ANSI / c-client / mmdf.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-11  |  12.6 KB  |  272 lines

  1. /*
  2.  * Program:    MMDF mail routines
  3.  *
  4.  * Author:    Bruce W. Knipe <bwk@cyberstore.ca> from bezerk.c by:
  5.  *        Mark Crispin
  6.  *        Networks and Distributed Computing
  7.  *        Computing & Communications
  8.  *        University of Washington
  9.  *        Administration Building, AG-44
  10.  *        Seattle, WA  98195
  11.  *        Internet: MRC@CAC.Washington.EDU
  12.  *
  13.  * Date:    15 May 1993
  14.  * Last Edited:    11 July 1993
  15.  *
  16.  * Copyright 1993 by the University of Washington
  17.  *
  18.  *  Permission to use, copy, modify, and distribute this software and its
  19.  * documentation for any purpose and without fee is hereby granted, provided
  20.  * that the above copyright notice appears in all copies and that both the
  21.  * above copyright notice and this permission notice appear in supporting
  22.  * documentation, and that the name of the University of Washington not be
  23.  * used in advertising or publicity pertaining to distribution of the software
  24.  * without specific, written prior permission.  This software is made
  25.  * available "as is", and
  26.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  27.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  28.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  29.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  30.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  32.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  33.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  34.  *
  35.  */
  36.  
  37. /* Validate line known to start with ``^A''
  38.  * Accepts: pointer to candidate string to validate as a From header
  39.  *        return pointer to end of date/time field
  40.  *        return pointer to offset from t of time (hours of ``mmm dd hh:mm'')
  41.  *        return pointer to offset from t of time zone (if non-zero)
  42.  * Returns: T if valid From string, t,ti,zn set; else NIL
  43.  */
  44.  
  45. #define VALID(s,x,ti,zn) \
  46. (s[1] == (char)0x01) && (s[2] == (char)0x01) && (s[3] == (char)0x01) && \
  47. (s[4] == '\n') && (s[5] == 'F' ) && \
  48. (s[6] == 'r' ) && (s[7] == 'o' ) && (s[8] == 'm' ) && (s[9] == ' ')  && \
  49.   (x = strchr (s+10,'\n')) && \
  50.   ((x-s < 46) || ((ti = ((x[-2] == ' ') ? -14 : (x[-3] == ' ') ? -15 : \
  51.              (x[-4] == ' ') ? -16 : (x[-5] == ' ') ? -17 : \
  52.              (x[-6] == ' ') ? -18 : (x[-7] == ' ') ? -19 : \
  53.              (x[-8] == ' ') ? -20 : (x[-9] == ' ') ? -21 : \
  54.              (x[-10]== ' ') ? -22 : (x[-11]== ' ') ? -23 : 0)) && \
  55.           (x[ti]   == ' ') && (x[ti+1] == 'r') && (x[ti+2] == 'e') && \
  56.           (x[ti+3] == 'm') && (x[ti+4] == 'o') && (x[ti+5] == 't') && \
  57.           (x[ti+6] == 'e') && (x[ti+7] == ' ') && (x[ti+8] == 'f') && \
  58.           (x[ti+9] == 'r') && (x[ti+10]== 'o') && (x[ti+11]== 'm') && \
  59.           (x += ti)) || T) && \
  60.   (x-s >= 27) && \
  61.   ((x[ti = -5] == ' ') ? ((x[-8] == ':') ? !(zn = 0) : \
  62.               ((x[ti = zn = -9] == ' ') || \
  63.                ((x[ti = zn = -11] == ' ') && \
  64.                 ((x[-10] == '+') || (x[-10] == '-'))))) : \
  65.    ((x[zn = -4] == ' ') ? (x[ti = -9] == ' ') : \
  66.     ((x[zn = -6] == ' ') && ((x[-5] == '+') || (x[-5] == '-')) && \
  67.      (x[ti = -11] == ' ')))) && \
  68.   (x[ti - 3] == ':') && (x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') && \
  69.   (x[ti - 3] == ' ') && (x[ti - 7] == ' ') && (x[ti - 11] == ' ')
  70.  
  71.  
  72. /* You are not expected to understand this macro, but read the next page if
  73.  * you are not faint of heart.
  74.  *
  75.  * Known formats to the VALID macro are:
  76.  *         From user Wed Dec  2 05:53 1992
  77.  * BSD        From user Wed Dec  2 05:53:22 1992
  78.  * SysV        From user Wed Dec  2 05:53 PST 1992
  79.  * rn        From user Wed Dec  2 05:53:22 PST 1992
  80.  *        From user Wed Dec  2 05:53 -0700 1992
  81.  *        From user Wed Dec  2 05:53:22 -0700 1992
  82.  *        From user Wed Dec  2 05:53 1992 PST
  83.  *        From user Wed Dec  2 05:53:22 1992 PST
  84.  *        From user Wed Dec  2 05:53 1992 -0700
  85.  * Solaris    From user Wed Dec  2 05:53:22 1992 -0700
  86.  *
  87.  * Plus all of the above with `` remote from xxx'' after it. Thank you very
  88.  * much, smail and Solaris, for making my life considerably more complicated.
  89.  */
  90.  
  91. /*
  92.  * What?  You want to understand the VALID macro anyway?  Alright, since you
  93.  * insist.  Actually, it isn't really all that difficult, provided that you
  94.  * take it step by step.
  95.  *
  96.  * Line 1    Validates that the 2-4th characters are CTRL/A.
  97.  * Line 2    Validates that the 5-6th characters are newline and ``F''.
  98.  * Line 3    Validates that the 7-10th characters are ``rom ''.
  99.  * Line 4    Sets x to point to the end of the line.
  100.  * Lines 5-14    First checks to see if the line is at least 46 characters long.
  101.  *        If so, it scans backwards up to 10 characters (the UUCP system
  102.  *        name length limit due to old versions of UNIX) to find a space.
  103.  *        If one is found, it backs up 12 characters from that point, and
  104.  *        sees if the string from there is `` remote from''.  If so, it
  105.  *        sets x to that position.  The ``|| T'' is there so the parse
  106.  *        will still continue.
  107.  * Line 15    Makes sure that there are at least 27 characters in the line.
  108.  * Lines 16-19    Checks if the date/time ends with the year.  If so, It sees if
  109.  *        there is a colon 3 characters further back; this would mean
  110.  *        that there is no timezone field and zn is set to 0 and ti is
  111.  *        left in front of the year.  If not, then it expects there to
  112.  *        either to be a space four characters back for a three-letter
  113.  *        timezone, or a space six characters back followed by a + or -
  114.  *        for a numeric timezone.  If a timezone is found, both zn and
  115.  *        ti are the offset of the space immediately before it.
  116.  * Lines 20-22    Are the failure case for a date/time not ending with a year in
  117.  *        line 14.  If there is a space four characters back, it is a
  118.  *        three-letter timezone; there must be a space for the year nine
  119.  *        characters back.  Otherwise, there must be a space six
  120.  *        characters back and a + or - five characters back to indicate a
  121.  *        numeric timezone and a space eleven characters back to indicate
  122.  *        a year.  zn and ti are set appropriately.
  123.  * Line 23    Make sure that the string before ti is of the form hh:mm or
  124.  *        hh:mm:ss.  There must be a colon three characters back, and a
  125.  *        space six or nine characters back (depending upon whether or
  126.  *        not the character six characters back is a colon).  ti is set
  127.  *        to be the offset of the space immediately before the time.
  128.  * Line 24    Make sure the string before ti is of the form www mmm dd.
  129.  *        There must be a space three characters back (in front of the
  130.  *        day), one seven characters back (in front of the month), and
  131.  *        one eleven characters back (in front of the day of week).
  132.  *
  133.  * Why a macro?  It gets invoked a *lot* in a tight loop.  On some of the
  134.  * newer pipelined machines it is faster being open-coded than it would be if
  135.  * subroutines are called.
  136.  *
  137.  * Why does it scan backwards from the end of the line, instead of doing the
  138.  * much easier forward scan?  There is no deterministic way to parse the
  139.  * ``user'' field, because it may contain unquoted spaces!  Yes, I tested it to
  140.  * see if unquoted spaces were possible.  They are, and I've encountered enough
  141.  * evil mail to be totally unwilling to trust that ``it will never happen''.
  142.  */
  143.  
  144. /* Build parameters */
  145.  
  146. #define KODRETRY 15        /* kiss-of-death retry in seconds */
  147. #define LOCKTIMEOUT 5        /* lock timeout in minutes */
  148. #define CHUNK 8192        /* read-in chunk size */
  149.  
  150.  
  151. /* Command bits from mmdf_getflags() */
  152.  
  153. #define fSEEN 1
  154. #define fDELETED 2
  155. #define fFLAGGED 4
  156. #define fANSWERED 8
  157.  
  158.  
  159. /* Status string */
  160.  
  161. #define STATUS "Status: RO\nX-Status: DFA\n\n"
  162.  
  163. /* MMDF per-message cache information */
  164.  
  165. typedef struct file_cache {
  166.   char *header;            /* pointer to RFC 822 header */
  167.   unsigned long headersize;    /* size of RFC 822 header */
  168.   char *body;            /* pointer to message body */
  169.   unsigned long bodysize;    /* size of message body */
  170.   char status[sizeof (STATUS)];    /* status flags */
  171.   char internal[1];        /* start of internal header and data */
  172. } FILECACHE;
  173.  
  174.  
  175. /* MMDF I/O stream local data */
  176.     
  177. typedef struct mmdf_local {
  178.   unsigned int dirty : 1;    /* disk copy needs updating */
  179.   int ld;            /* lock file descriptor */
  180.   char *name;            /* local file name for recycle case */
  181.   char *lname;            /* lock file name */
  182.   off_t filesize;        /* file size parsed */
  183.   time_t filetime;        /* last file time */
  184.   unsigned long cachesize;    /* size of local cache */
  185.   FILECACHE **msgs;        /* pointers to message-specific information */
  186.   char *buf;            /* temporary buffer */
  187.   unsigned long buflen;        /* current size of temporary buffer */
  188. } MMDFLOCAL;
  189.  
  190.  
  191. /* Convenient access to local data */
  192.  
  193. #define LOCAL ((MMDFLOCAL *) stream->local)
  194.  
  195. /* Function prototypes */
  196.  
  197. DRIVER *mmdf_valid (char *name);
  198. int mmdf_isvalid (char *name,char *tmp);
  199. void *mmdf_parameters (long function,void *value);
  200. void mmdf_find (MAILSTREAM *stream,char *pat);
  201. void mmdf_find_bboards (MAILSTREAM *stream,char *pat);
  202. void mmdf_find_all (MAILSTREAM *stream,char *pat);
  203. void mmdf_find_all_bboards (MAILSTREAM *stream,char *pat);
  204. long mmdf_subscribe (MAILSTREAM *stream,char *mailbox);
  205. long mmdf_unsubscribe (MAILSTREAM *stream,char *mailbox);
  206. long mmdf_subscribe_bboard (MAILSTREAM *stream,char *mailbox);
  207. long mmdf_unsubscribe_bboard (MAILSTREAM *stream,char *mailbox);
  208. long mmdf_create (MAILSTREAM *stream,char *mailbox);
  209. long mmdf_delete (MAILSTREAM *stream,char *mailbox);
  210. long mmdf_rename (MAILSTREAM *stream,char *old,char *new);
  211. MAILSTREAM *mmdf_open (MAILSTREAM *stream);
  212. void mmdf_close (MAILSTREAM *stream);
  213. void mmdf_fetchfast (MAILSTREAM *stream,char *sequence);
  214. void mmdf_fetchflags (MAILSTREAM *stream,char *sequence);
  215. ENVELOPE *mmdf_fetchstructure (MAILSTREAM *stream,long msgno,BODY **body);
  216. char *bezerk_snarf (MAILSTREAM *stream,long msgno,long *size);
  217. char *mmdf_fetchheader (MAILSTREAM *stream,long msgno);
  218. char *mmdf_fetchtext (MAILSTREAM *stream,long msgno);
  219. char *mmdf_fetchbody(MAILSTREAM *stream,long m,char *sec,unsigned long *len);
  220. void mmdf_setflag (MAILSTREAM *stream,char *sequence,char *flag);
  221. void mmdf_clearflag (MAILSTREAM *stream,char *sequence,char *flag);
  222. void mmdf_search (MAILSTREAM *stream,char *criteria);
  223. long mmdf_ping (MAILSTREAM *stream);
  224. void mmdf_check (MAILSTREAM *stream);
  225. void mmdf_expunge (MAILSTREAM *stream);
  226. long mmdf_copy (MAILSTREAM *stream,char *sequence,char *mailbox);
  227. long mmdf_move (MAILSTREAM *stream,char *sequence,char *mailbox);
  228. long mmdf_append (MAILSTREAM *stream,char *mailbox,STRING *message);
  229. void mmdf_gc (MAILSTREAM *stream,long gcflags);
  230.  
  231. void mmdf_abort (MAILSTREAM *stream);
  232. char *mmdf_file (char *dst,char *name);
  233. int mmdf_lock (char *file,int flags,int mode,char *lock,int op);
  234. void mmdf_unlock (int fd,MAILSTREAM *stream,char *lock);
  235. int mmdf_parse (MAILSTREAM *stream,char *lock,int op);
  236. char *mmdf_eom (char *som,char *sod,long i);
  237. int mmdf_extend (MAILSTREAM *stream,int fd,char *error);
  238. void mmdf_save (MAILSTREAM *stream,int fd);
  239. int mmdf_copy_messages (MAILSTREAM *stream,char *mailbox);
  240. void mmdf_write_message (struct iovec iov[],int *i,FILECACHE *m);
  241. void mmdf_update_status (char *status,MESSAGECACHE *elt);
  242. short mmdf_getflags (MAILSTREAM *stream,char *flag);
  243. char mmdf_search_all (MAILSTREAM *stream,long msgno,char *d,long n);
  244. char mmdf_search_answered (MAILSTREAM *stream,long msgno,char *d,long n);
  245. char mmdf_search_deleted (MAILSTREAM *stream,long msgno,char *d,long n);
  246. char mmdf_search_flagged (MAILSTREAM *stream,long msgno,char *d,long n);
  247. char mmdf_search_keyword (MAILSTREAM *stream,long msgno,char *d,long n);
  248. char mmdf_search_new (MAILSTREAM *stream,long msgno,char *d,long n);
  249. char mmdf_search_old (MAILSTREAM *stream,long msgno,char *d,long n);
  250. char mmdf_search_recent (MAILSTREAM *stream,long msgno,char *d,long n);
  251. char mmdf_search_seen (MAILSTREAM *stream,long msgno,char *d,long n);
  252. char mmdf_search_unanswered (MAILSTREAM *stream,long msgno,char *d,long n);
  253. char mmdf_search_undeleted (MAILSTREAM *stream,long msgno,char *d,long n);
  254. char mmdf_search_unflagged (MAILSTREAM *stream,long msgno,char *d,long n);
  255. char mmdf_search_unkeyword (MAILSTREAM *stream,long msgno,char *d,long n);
  256. char mmdf_search_unseen (MAILSTREAM *stream,long msgno,char *d,long n);
  257. char mmdf_search_before (MAILSTREAM *stream,long msgno,char *d,long n);
  258. char mmdf_search_on (MAILSTREAM *stream,long msgno,char *d,long n);
  259. char mmdf_search_since (MAILSTREAM *stream,long msgno,char *d,long n);
  260. char mmdf_search_body (MAILSTREAM *stream,long msgno,char *d,long n);
  261. char mmdf_search_subject (MAILSTREAM *stream,long msgno,char *d,long n);
  262. char mmdf_search_text (MAILSTREAM *stream,long msgno,char *d,long n);
  263. char mmdf_search_bcc (MAILSTREAM *stream,long msgno,char *d,long n);
  264. char mmdf_search_cc (MAILSTREAM *stream,long msgno,char *d,long n);
  265. char mmdf_search_from (MAILSTREAM *stream,long msgno,char *d,long n);
  266. char mmdf_search_to (MAILSTREAM *stream,long msgno,char *d,long n);
  267.  
  268. typedef char (*search_t) (MAILSTREAM *stream,long msgno,char *d,long n);
  269. search_t mmdf_search_date (search_t f,long *n);
  270. search_t mmdf_search_flag (search_t f,char **d);
  271. search_t mmdf_search_string (search_t f,char **d,long *n);
  272.